﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CosmosKernel4.Global
{
    class StringManip
    {
        public int basic_c2n(char c)
        {
            int r = 0;
            switch (c)
            {
                case '0':
                    r = 0;
                    break;
                case '1':
                    r = 1;
                    break;
                case '2':
                    r = 2;
                    break;
                case '3':
                    r = 3;
                    break;
                case '4':
                    r = 4;
                    break;
                case '5':
                    r = 5;
                    break;
                case '6':
                    r = 6;
                    break;
                case '7':
                    r = 7;
                    break;
                case '8':
                    r = 8;
                    break;
                case '9':
                    r = 9;
                    break;
                default:
                    r = 0;
                    break;
          
            }
            return r;
        }
        public static string stripchar(string str, char character)
        {
            string r = str;
            while (r.IndexOf(character) != -1)
            {
                r = r.Remove(r.IndexOf(character), 1);
            }
            return r;
        }
        public static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

        public static string GetString(byte[] bytes)
        {
            char[] chars = new char[bytes.Length / sizeof(char)];
            System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
            return new string(chars);
        }
        public static int GetNthIndex(string s, char t, int n)
        {
            int count = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == t)
                {
                    count++;
                    if (count == n)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }
    }
}
